import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Random;

@WebServlet("/Register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        generateCode(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        String vcode=request.getParameter("vcode");//用户输入的验证码
        String scode=(String)request.getSession().getAttribute("code");//生成的验证码
        if(!scode.equals(vcode)){
            request.setAttribute("msg","验证码错误");
            request.getRequestDispatcher("/register.jsp").forward(request,response);

        }
        ServletContext context=request.getServletContext();
        Object userInDB=context.getAttribute("users");
        HashMap<String,String> users;
        if (userInDB == null) {
            users=new HashMap();
            users.put(username,password);
            context.setAttribute("users",users);
            request.setAttribute("msg","注册成功!");
            request.getRequestDispatcher("/Login.jsp").forward(request,response);
            }else {
            users=(HashMap<String, String>) userInDB;
            if(users.containsKey(username)){
                request.setAttribute("msg","用户名已存在");
                request.getRequestDispatcher("/register.jsp").forward(request,response);

            }else{
                users.put(username,password);
                context.setAttribute("users",users);
                request.setAttribute("msg","注册成功！");
                request.getRequestDispatcher("/Login.jsp").forward(request,response);
            }
        }
        }
    private void generateCode(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        Random random = new Random(System.currentTimeMillis());
        StringBuilder sb = new StringBuilder();

        char sc = 'a';
        for (int i = 0; i <= 4; i++) {
            int str = random.nextInt(62);
            if (str < 10) {
                sb.append(str);
            } else if (str < 36) {
                sc = (char) ((str - 10) + 'a');
                sb.append(sc);
            } else {
                sc = (char) ((str - 36) + 'A');
                sb.append(sc);
            }
        }

        BufferedImage image = new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.setColor(Color.GRAY);
        graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
        graphics.setColor(Color.blue);
        graphics.setFont(new Font("Time", Font.BOLD, 18));
        graphics.drawString(sb.toString(), 3, 17);
        graphics.dispose();
        ImageIO.write(image, "JPG", response.getOutputStream());
        System.out.println("生成的验证码：" + sb);

        // 将验证码保存到session
        request.getSession().setAttribute("code", sb.toString());
    }
}
